using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using System;
public class VHScrollRect : ScrollRect
{
    public ScrollRect parentScroll;
    public bool CanScroll = true;
    ScrollRect scroll;
    
    public int test;
    public bool isVertical = false;

    private bool isSelf = false;
   public Action begeinDragDel;
    public Action endDragDel;
    protected override void Awake()
    {
        parentScroll = transform.parent.parent.parent.GetComponent<ScrollRect>();
        scroll = GetComponent<ScrollRect>();
    }
    public override void OnBeginDrag(PointerEventData eventData)
    {
        Vector2 touchDeltaPosition;
#if UNITY_EDITOR
        float delta_x = Input.GetAxis("Mouse X");
        float delta_y = Input.GetAxis("Mouse Y");
        touchDeltaPosition = new Vector2(delta_x, delta_y);
#endif

#if !UNITY_EDITOR
        touchDeltaPosition = Input.GetTouch(0).deltaPosition;
#endif
        if (isVertical)
        {
            if (Mathf.Abs(touchDeltaPosition.x) < Mathf.Abs(touchDeltaPosition.y))
            {
                isSelf = true;
                base.OnBeginDrag(eventData);
            }
            else
            {
                isSelf = false;
                parentScroll.OnBeginDrag(eventData);
                if (begeinDragDel != null)
                    begeinDragDel();
            }
        }
        else
        {
            if (Mathf.Abs(touchDeltaPosition.x) > Mathf.Abs(touchDeltaPosition.y))
            {
                isSelf = true;
                base.OnBeginDrag(eventData);
            }
            else
            {
                isSelf = false;
                parentScroll.OnBeginDrag(eventData);
                if (begeinDragDel != null)
                    begeinDragDel();
            }
        }
    
    }


    public override void OnDrag(PointerEventData eventData)
    {
        if (!CanScroll)
            return;
        if (isSelf)
        {
            base.OnDrag(eventData);
        }
        else
        {
            parentScroll.OnDrag(eventData);
        }
    }



    public override void OnEndDrag(PointerEventData eventData)
    {
        if (isSelf)
        {
            base.OnEndDrag(eventData);
        }
        else
        {
            parentScroll.OnEndDrag(eventData);
            if (endDragDel != null)
                endDragDel();
        }
    
    }
    public static List<T> GetChilds<T>(Transform t, bool IsObjectActive = true)
    {
        List<T> list = new List<T>();
        for (int i = 0; i < t.childCount; i++)
        {
            if (IsObjectActive || t.GetChild(i).gameObject.activeInHierarchy)
                list.Add(t.GetChild(i).GetComponent<T>());
        }
        return list;
    }
   public void SetContent()
    {
        int count = GetChilds<Transform>(transform,false).Count;
        GridLayoutGroup grid = scroll.content.GetComponent<GridLayoutGroup>();
        int AllColumn = Mathf.CeilToInt(count * 1f / grid.constraintCount);
        Vector2 size = scroll.content.sizeDelta;
        size.y = grid.cellSize.y * AllColumn
              + grid.spacing.y * (AllColumn + 1)
              + grid.padding.top + grid.padding.bottom;
        scroll.content.sizeDelta = size;
    }
}
